package com.wendyxie.sproutup.view

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wendyxie.sproutup.databinding.ActivityUserScoreRecordBinding
import com.wendyxie.sproutup.db.SQLInitHelper
import com.wendyxie.sproutup.db.SQLUserScoreRecordHelper
import com.wendyxie.sproutup.entity.UserScoreRecordEntity
import com.wendyxie.sproutup.view.adapter.UserScoreRecordAdapter


class UserScoreRecordActivity : AppCompatActivity() {

    private lateinit var binding: ActivityUserScoreRecordBinding
    private lateinit var scoreAdapter: UserScoreRecordAdapter
    private var currentPage = 1
    private val pageSize = 10
    private var isLoadingMore = false // 是否正在加载更多数据

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityUserScoreRecordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 列表适配器
        scoreAdapter = UserScoreRecordAdapter()
        binding.listItem.layoutManager = LinearLayoutManager(this)
        binding.listItem.adapter = scoreAdapter
        val dividerItemDecoration = DividerItemDecoration(binding.listItem.getContext(),DividerItemDecoration.VERTICAL)
        binding.listItem.addItemDecoration(dividerItemDecoration)
        // 添加滚动监听器
        binding.listItem.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val totalItemCount = scoreAdapter.itemCount
                val lastVisibleItem = (binding.listItem.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                if (!isLoadingMore && lastVisibleItem >= totalItemCount - 1) {
                    recyclerView.post{
                        loadMoreData()
                    }
                }
            }
        })
        // 添加刷新监听器
        binding.swipeRefreshLayout.setOnRefreshListener {
            refreshView()
        }
        // 加载数据
        refreshView()
    }

    private fun refreshView() {
        currentPage = 1
        isLoadingMore = false
        scoreAdapter.clearItems()
        loadData(currentPage, pageSize)
        binding.swipeRefreshLayout.isRefreshing = false
    }

    private fun loadMoreData() {
        currentPage++
        loadData(currentPage, pageSize)
    }

    private fun loadData(currentPage: Int, pageSize: Int) {
        Log.d("UserScoreRecordActivity", "loadData currentPage: $currentPage")
        if (isLoadingMore) {
            return
        }
        isLoadingMore = true
        val moreRecords = SQLUserScoreRecordHelper.queryRecordList(UserScoreRecordEntity(currentPage, pageSize), SQLInitHelper(this).openDatabase())
        scoreAdapter.addItems(moreRecords)
        // 查询到的没有数据，则说明不需要继续加载
        isLoadingMore = moreRecords.isEmpty()
    }
}
